<?php
namespace Home\Controller;
use Common\Utils\Constants;
use Common\Utils\WeChatUtils;
use Think\Controller;

header("Content-Type:text/html; charset=utf-8");

class BaseController extends Controller
{
    protected $baseModel;
    /**
     * 判断是否登录
     * BaseController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $user = $this->getUser();
//        $APPID = C('APPID');
//        $APPSECRET = C('APPSECRET');
//        $TOKEN = C('TOKEN');
////       session_destroy();
//        $utils = new WeChatUtils($APPID, $APPSECRET, $TOKEN);
//        if (empty($user['id'])) {
//            $utils->getUserCode('0');
//            exit();
//        }
//        $user = D('Users')->where(array('user_id'=>$user['user_id']))->find();
//        $user['id'] = 752;
//        $user['id'] = 727;
        if (!$user || !isset($user['id']) || empty($user['id'])) {
//            $utils->getUserCode('0');
//            exit();
            $this->redirect('/Home/Login/loginUI');
            return;
        }
        $this->assign('system_name',$this->getSystemName());
//        session('user', $user);
//        $this->assign('user',$user);
    }

    protected function getSystemName(){
        $system_name = M('Config')->where(array('name'=>Constants::CONFIG_SYSTEM_NAME))->getField('value');
        return $system_name;
    }

    protected function getUser(){
        $user = cookie('user');
//        var_dump($user);
        $user = D('User')->where(array('id'=>$user['id']))->find();
        return $user;
    }

    protected function getUploadObj($uploadDir)
    {
        $path = './Public/upload/'.$uploadDir.'/';
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 553145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = $path; // 设置附件上传根目录
        $upload->savePath = 'Images/'; // 设置附件上传（子）目录
        if (!is_dir($path)) {
            mkdir($path, 0755, true);
        }
        // 上传文件
        return array('upload'=>$upload,'path'=>'/Public/upload/'.$uploadDir.'/');
    }

    protected function jsSDK(){
        $APPID = C('APPID');
        $APPSECRET = C('APPSECRET');
        $TOKEN = C('TOKEN');
        $utils = new WeChatUtils($APPID, $APPSECRET, $TOKEN);
        $ticket = $utils->getJsApiTicket();
        $noncestr = $utils->getRandCode();//获取随机码
        $timestamp = time();//时间戳
        // 注意 URL 一定要动态获取，不能 hardcode.
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $user = cookie('user');
//        $url = U('Home/WeChat/invite/'.$user['id']);
        $str = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '&timestamp=' . $timestamp . '&url='.$url;
        $signature = sha1($str);
        $this->assign('host',"$protocol$_SERVER[HTTP_HOST]");
        $this->assign('appId',$APPID);
        $this->assign('user',$user);
        $this->assign('ticket',$ticket);
        $this->assign('noncestr',$noncestr);
        $this->assign('timestamp',$timestamp);
        $this->assign('signature',$signature);
        $this->assign('BASE_URL',C('BASE_URL'));//域名
    }

    /**
     * @param $success
     * @param $message
     * @param int $code
     * @param array $data
     */
    protected function ajax($success,$message,$code=-1,$data=array()){
        $this->ajaxReturn(array(Constants::SUCCESS=>$success,Constants::MESSAGE=>$message,'data'=>$data,'code'=>$code));
    }
}